package com.mj.Set.list;


/**
 * @Author: LDeng
 * @Date: 2021-02-19 15:12
 */
public class LinkedList<E> extends AbstractList<E> {

    private Node<E> first;
    private Node<E> last;


    private static class Node<E> {
        Node<E> prev;
        E element;
        Node<E> next;


        public Node(Node<E> prev, E element, Node<E> next) {
            this.prev = prev;
            this.element = element;
            this.next = next;
        }

        @Override
        public String toString() {
            StringBuilder sb=new StringBuilder();
            if(prev!=null){
                sb.append(prev.element);
            }else {
                sb.append("null");
            }

            sb.append("<").append(element).append(">");

            if(next!=null){
                sb.append(next.element);
            }else {
                sb.append("null");
            }

            return sb.toString();
        }
    }

    @Override
    public void clear() {
        size = 0;
        first = null;
        last = null;
    }

    @Override
    public void add(int index, E element) {
        rangeCheckForAdd(index);
        if (index == size) {//向最后位置添加
            Node<E> oldLast = last;//拿出原来的last保存
            last = new Node<>(oldLast, element, null);//上一个之前的last, 下一个null
            if (oldLast == null) {//空链表， 链表的第一个元素
                first = last;//last指向的节点交给first去指向
            } else {
                oldLast.next = last;//原last 的next 指向新的last
            }
        } else {//非最后位置添加
            Node<E> next = getNodeByIndex(index);//原节点变成新添加节点的下一个节点
            Node<E> prev = next.prev;//原节点的上一个节点变成新添加节点的上一个节点
            Node<E> node = new Node<>(prev, element, next);//构造的时候已经有了新节点上一个节点指向原上一个节点，新节点的下一个节点指向原下一个节点
            next.prev = node;//原下一个节点的上一个节点指向新插入的节点
            if (prev == null) { //index=0
                first = node;
            } else {
                prev.next = node;//原上一个节点的下一个节点指向新插入的节点
            }
        }
        size++;
    }

    @Override
    public E remove(int index) {
        rangeCheck(index);
        Node<E> node = getNodeByIndex(index);
        Node<E> prev = node.prev;
        Node<E> next = node.next;
        if (prev == null) {//即第一个节点 index=0
            first = next;
        } else {
            prev.next = next;
        }

        if (next == null) {//即最后一个节点 index=size-1
            last = prev;
        } else {
            next.prev = prev;
        }

        size--;
        return node.element;
    }

    @Override
    public int indexOf(E element) {
        if (element == null) {
            Node<E> node = first;
            for (int i = 0; i < size; i++) {
                if (node.element == null) return i;
                node = node.next;
            }
        } else {
            Node<E> node = first;
            for (int i = 0; i < size; i++) {
                if (element.equals(node.element)) return i;
                node = node.next;
            }
        }
        return ELEMENT_NOT_FOUND;
    }

    @Override
    public E get(int index) {
        return getNodeByIndex(index).element;
    }

    @Override
    public E set(int index, E element) {
        Node<E> node = getNodeByIndex(index);
        E old = node.element;
        node.element = element;
        return old;
    }

    //获取索引位置的节点对象
    private Node<E> getNodeByIndex(int index) {
        rangeCheck(index);
        if (index < (size >> 1)) {//index小于size的一半
            Node<E> node = first;
            for (int i = 0; i < index; i++) {
                node = node.next;
            }
            return node;
        } else {
            Node<E> node = last;
            for (int i = size - 1; i > index; i--) {
                node = node.prev;
            }
            return node;
        }
    }

    @Override
    public String toString() {
        StringBuilder string = new StringBuilder();
        string.append("size=").append(size).append(", [");
        Node<E> node = first;
        for (int i = 0; i < size; i++) {
            if (i != 0) {
                string.append(", ");
            }
            string.append(node);
            node = node.next;
        }
        string.append("]");
        return string.toString();
    }
}
